import Vue from 'vue'
import VueRouter from 'vue-router'
import Manager from '../views/Manager.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/ai',
    component: Manager,
    children:[
      {path:'ai',meta:{name: '作图'},component: ()=>import('../views/main/AIDraw.vue')},
      {path: 'imgRecognize', meta: {name: '人脸识别'}, component: () => import('../views/main/ImgRecognize.vue')},
      {path:'peopleToAnime',meta:{name: '人像动漫化'},component: ()=>import('../views/main/PeopleToAnime.vue')},
      {path:'tran',meta:{name: '平滑变化'},component: ()=>import('../views/main/transition.vue')},
      {path:'stylisticShifts',meta:{name: '风格转换'},component: ()=>import('../views/main/StylisticShifts.vue')},
    ]
  },
  {
    path: '/403',
    name: '403View',
    component: ()=>import('../views/403.vue'),
    meta:{name: '无权限'},
  },
  {
    path: '*',
    name: '404View',
    component: ()=>import('../views/404.vue'),
    meta:{name: '找不到页面'},
  },
  {
    path: '/login',
    name: 'LoginView',
    component: ()=>import('../views/LoginView.vue'),
    meta:{name: '登录'},
  },
  {
    path: '/register',
    name: 'RegisterView',
    component: ()=>import('../views/Register.vue'),
    meta:{name: '注册'},
  },
]

const router = new VueRouter({
  mode: 'history',
  routes
})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 路由守卫
//to:去哪里的信息，from是从哪里的信息，next是必须要执行的下一步
router.beforeEach((to,from,next)=>{
  let adminPaths=['/user']
  let loginUser=JSON.parse(localStorage.getItem("user")) || {}
  //不能让普通用户到管理用户页面
  if(loginUser.role==='用户'&&adminPaths.includes(to.path))
    next('/403')
  else
    next()
})

export default router
